<?php

class UserRoleTemplate extends TTemplateControl
{
	public $user_manager;
	public $role_manager;
	public $user_role_manager;
	
	public function __construct()
	{
		parent::__construct();
		$this->user_manager = $this->Application->Modules['users'];
		$this->role_manager = $this->Application->Modules['roles'];
		$this->user_role_manager = $this->Application->Modules['users_roles'];
	}
	
	public function onLoad($params)
	{
		parent::onLoad($params);
		
		$this->RoleList->DataSource = $this->role_manager->getRolesList();
		$this->RoleList->dataBind();
	}
	
	public function setUserId($user_id)
	{
		$this->setViewState('user_id', $user_id, 0);
	}
	
	public function getUserId()
	{
		return $this->getViewState('user_id', 0);
	}
	
	public function checkBoxClicked($sender, $param)
	{
		$role_id = $sender->getValue();
		$user = $this->user_manager->getUserById($this->getUserId());
		
		if($user == null)
			return;
		if($this->user_role_manager->hasUserRole($user->getId(), $role_id))
			$this->user_role_manager->deleteUserRole($user->getId(), $role_id);
		else
			$this->user_role_manager->addUserRole($user->getId(), $role_id);
	}
}

?>